home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / RELEASE.ZIP / sub_arctic / anim / time_interval.java < prev    next >
Encoding:
Java Source  |  1996-10-04  |  10.4 KB  |  356 lines

  1. package sub_arctic.anim;
  2. import sub_arctic.lib.interactor;
  3.  
  4. /**
  5.  * This class is handy for manipulating transitions both by the user
  6.  * and by the animation agent.  It keeps track of when a transition 
  7.  * is to occur and allows you to specify a time_interval either in
  8.  * absolute time or in various relative ways.<p>
  9.  *
  10.  * @author Ian Smith
  11.  */
  12. public class time_interval {
  13.  
  14.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  15.  
  16.   /**
  17.    * Did they specify a start time.
  18.    */
  19.   protected boolean _specified_start;
  20.  
  21.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  22.  
  23.   /**
  24.    * Did they specify an end time.
  25.    */
  26.   protected boolean _specified_end;
  27.  
  28.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  29.  
  30.   /**
  31.    * Did they specify this in terms of duration?
  32.    */
  33.   protected boolean _used_duration;
  34.  
  35.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  36.  
  37.   /**
  38.    * Return if the user specified this interval in terms of duration.
  39.    * @return boolean true if the user specified a duration
  40.    */
  41.   public boolean used_duration() { return _used_duration;};
  42.  
  43.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  44.  
  45.   /**
  46.    * Return if the user specified a start time for this interval.
  47.    * @return boolean true if the user specified a start time
  48.    */
  49.   public boolean specified_start() { return _specified_start;}
  50.  
  51.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  52.  
  53.   /**
  54.    * Return if the user specified an end time for this interval.
  55.    * @return boolean true if the user specified an end time
  56.    */
  57.   public boolean specified_end() { return _specified_end;};
  58.  
  59.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  60.  
  61.   /**
  62.    * If used _used_duration is true, this will contain the duration. 
  63.    */
  64.   protected long _duration;
  65.  
  66.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  67.  
  68.   /**
  69.    * Return how long this interval is.  Note that if you specified this
  70.    * as a duration, we return it, otherwise we calculate it. We DO NOT
  71.    * check to see if your start and end time are as yet known, so you
  72.    * would be well advised to check _used_duration and valid() to 
  73.    * see if its ok to call this.<p>
  74.    * 
  75.    * @return long the duration of this interval in milliseconds
  76.    */
  77.   public long duration() { 
  78.     if (_used_duration==true) {
  79.       return _duration;
  80.     }
  81.     return _end_time-_start_time;
  82.   }
  83.  
  84.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  85.  
  86.   /**
  87.    * We are not in a valid state to be executed until we
  88.    * have both and end and start time.<p>
  89.    *
  90.    * @return boolean true if the user has specified a start and end time
  91.    */
  92.   boolean valid() {
  93.     return (_specified_start && _specified_end);
  94.   }
  95.  
  96.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  97.  
  98.   /**
  99.    * What's the current time in milliseconds?<p>
  100.    * @return long the current time 
  101.    */
  102.   public static long now() {
  103.     return System.currentTimeMillis();
  104.   }
  105.  
  106.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  107.  
  108.   /**
  109.    * Use this constant to express that this time interval
  110.    * starts after the start of some other transition.
  111.    */
  112.   public final static int AFTER_START_OF=0;
  113.  
  114.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  115.  
  116.   /**
  117.    * Use this constant to express that this time interval
  118.    * starts after the end of some other transition.
  119.    */
  120.   public final static int AFTER_END_OF=1;
  121.  
  122.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  123.  
  124.   /**
  125.    * Start (if known) is here 
  126.    */
  127.   protected long _start_time;
  128.  
  129.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  130.  
  131.   /**
  132.    * End (if known) is here 
  133.    */
  134.   protected long _end_time;
  135.  
  136.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  137.  
  138.   /**
  139.    * Retrieve the start time of this interval. You may want to 
  140.    * check to make sure this interval has a start time before
  141.    * calling this.<p> 
  142.    *
  143.    * @return long the start time of this interval  in milliseconds
  144.    */
  145.   public long start_time() { return _start_time;};
  146.  
  147.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  148.  
  149.   /**
  150.    * Retrieve the end time of this interval. You may want to 
  151.    * check to make sure this interval has an end time before
  152.    * calling this. <p>
  153.    *
  154.    * @return long the end time of this interval  in milliseconds
  155.    */
  156.   public long end_time() { return _end_time;};
  157.  
  158.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  159.  
  160.   /**
  161.    * If we are relative, who are we relative to?
  162.    */
  163.   protected transition  _related_to;
  164.  
  165.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  166.  
  167.   /**
  168.    * Retrieve what transition we are related to, if any.
  169.    * @return transition the transition to which we are related via 
  170.    *                    AFTER_START_OF or AFTER_END_OF.
  171.    */
  172.   public transition related_to() { return _related_to;};
  173.  
  174.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  175.  
  176.   /**
  177.    * If we are relative, how are we relative? Will be one of the 
  178.    * constants AFTER_START_OF or AFTER_END_OF.
  179.    */
  180.   protected int _related_how;
  181.  
  182.   /**
  183.    * This will return one of the relationship constants AFTER_START_OF
  184.    * or AFTER_END_OF if this time_interval is related to another 
  185.    * transition. <p>
  186.    *
  187.    * @return int one of the constants AFTER_START_OF and AFTER_END_OF
  188.    */
  189.   public int related_how() { return _related_how;};
  190.  
  191.   /**
  192.    * Return the status of whether or not this time_interval is related
  193.    * to other transitions.<p>
  194.    *
  195.    * @return boolean true if this object is started/ended with respect to 
  196.    *                 other objects.
  197.    */
  198.   boolean relative_to_others() {
  199.     return (related_to()!=null);
  200.   }
  201.  
  202.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  203.  
  204.   /*
  205.    * This delay is the amount of time after the start or end
  206.    * time if they specified a related-to type time_interval.
  207.    */
  208.   protected long _delay;
  209.  
  210.   /**
  211.    * Retrieve the amount of time after the start or end of the
  212.    * other transition if we are related to other transitions.
  213.    * @return long the amount of delay after the start or end of the other 
  214.    *              transition.
  215.    */
  216.   public long delay() { return _delay;}
  217.  
  218.   /**
  219.    * Set the delay after a relative transition.
  220.    * @param long d the new delay value
  221.    */
  222.   public void set_delay(long d) {_delay = d;}
  223.  
  224.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  225.  
  226.   /**
  227.    * Create one of these puppies... This creates a relative
  228.    * transition... you should supply either AFTER_START_OF
  229.    * and AFTER_END_OF as the "how" parameter. The parm is
  230.    * number of milliseconds after the start or end.<p>
  231.    * 
  232.    * @param int how how to be related to the supplied transition
  233.    * @param transition t the transition we are related to
  234.    * @param long parm the amount of delay after the start or end of the 
  235.    *                  related to transition
  236.    */
  237.   public time_interval(int how, transition t,long parm) {
  238.  
  239.     _related_to=t;
  240.     _related_how=how;
  241.     _delay=Math.max(parm,0);
  242.  
  243.     /* they haven't set the values for the object yet, the scheduler will */
  244.     _specified_start=false;
  245.     _specified_end=false;
  246.     _used_duration=false;
  247.   }
  248.  
  249.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  250.  
  251.   /**
  252.    * Create a time_interval with a known start and end point. These
  253.    * are in milliseconds.<p>
  254.    *
  255.    * @param long start the start time
  256.    * @param long end the end time
  257.    */
  258.   public time_interval(long start, long end) {
  259.     _start_time=start;
  260.     _end_time=end;
  261.     _specified_start=true;
  262.     _specified_end=true;
  263.     _used_duration=false;
  264.   }
  265.  
  266.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  267.  
  268.   /**
  269.    * Create a time interval that you will fill in the values for later.
  270.    * 
  271.    */
  272.   public time_interval() {
  273.     _specified_start=false;
  274.     _specified_end=false;
  275.     _used_duration=false;
  276.   }
  277.  
  278.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  279.  
  280.   /** 
  281.    * Set the interval of ending time to be a duration in milliseconds.<p>
  282.    *
  283.    * @param long millis the new duration in milliseconds
  284.    */
  285.   public void set_duration(long millis) {
  286.     _duration=millis;
  287.     _used_duration=true;
  288.   }
  289.  
  290.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  291.  
  292.   /**
  293.    * Set the interval ending time to be a particular value in milliseconds.<p>
  294.    * 
  295.    * @param long millis the new ending time in milliseconds.
  296.    */
  297.   public void set_ending_time(long millis) {
  298.     _specified_end=true;
  299.     _end_time=millis;
  300.   }
  301.  
  302.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  303.  
  304.   /**
  305.    * Set the ending time to be a particular amount of time from now.
  306.    * @param long millis the new ending point in time (distance from now in 
  307.    *                    milliseconds).
  308.    */
  309.   public void set_ending_time_from_now(long millis) {
  310.     _specified_end=true;
  311.     _end_time=now()+millis;
  312.   }
  313.  
  314.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  315.  
  316.   /**
  317.    * Set the start point to be a known point in milliseconds.<p>
  318.    *
  319.    * @param long millis the new start time in milliseconds
  320.    */
  321.   public void set_start_time(long millis) {
  322.     _specified_start=true;
  323.     _start_time=millis;
  324.   }
  325.  
  326.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  327.  
  328.   /**
  329.    * Set the start point to be a know point in millis from now.<p>
  330.    * 
  331.    * @param long millis the new start time (distance from now in milliseconds)
  332.    */
  333.   public void set_start_time_from_now(long millis) {
  334.     _specified_start=true;
  335.     _start_time=now()+ millis;
  336.   }
  337.  
  338.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  339. }
  340. /*=========================== COPYRIGHT NOTICE ===========================
  341.  
  342. This file is part of the subArctic user interface toolkit.
  343.  
  344. Copyright (c) 1996 Scott Hudson and Ian Smith
  345. All rights reserved.
  346.  
  347. The subArctic system is freely available for most uses under the terms
  348. and conditions described in 
  349.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  350. and appearing in full in the lib/interactor.java source file.
  351.  
  352. The current release and additional information about this software can be 
  353. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  354.  
  355. ========================================================================*/
  356.